#ifndef __POSTGRE_DATABASE_H__
  #define __POSTGRE_DATABASE_H__

#include "database.h"

#include "libpq-fe.h" // PostgreSQL header file

using namespace std;

//! Wrapper for PostgreSQL database

/*!
This class is inherited from database and offers access to a PostgreSQL database system.

Implementation date: 2008

Usage:

\code
record_set rs;
postgre_database pd;
pd.connect("127.0.0.1", "postgres", "passme", "postgres", "protocol_file.txt");
pd.execute_query("SELECT * FROM \"T1\" LIMIT 10;", rs);
cout << rs << endl;
\endcode
*/

class postgre_database : public database {
private:
  PGconn *connection;

  //! checks the status (may throw an exception)
  void check_status(PGresult *result, ExecStatusType type, bool clear_result = true, bool end_transaction_on_failure = true); // may throw a db_exception, also may rollback a transaction

public:
  virtual void connect(const string &connection_string, const string &protocol_file = global_empty_string);
  virtual void connect(const string &host, const string &user_id, const string &password, const string &db_name, const string &protocol_file = global_empty_string);
  virtual void disconnect();

  virtual bool is_connected();

  virtual void execute_query(const string &s, record_set &rs, bool append_to_record_set = false);
  virtual void execute_update(const string &s);

  virtual void start_transaction();
  virtual void commit();
  virtual void rollback();

  // the table must have an id column (bigint) as the primary-key
  virtual void lock_table(const string &table_name);
  virtual void unlock_table(const string &table_name);

  //! default constructor
  postgre_database();

  //! destructor
  virtual ~postgre_database();
}; // class postgre_database

#endif
